﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Biblioteca.Logica_Negocio.Domain_Model;
using Biblioteca.Acceso_a_Datos.Data_Mappers;
using PresentacionBiblioWeb.Models;

namespace PresentacionBiblioWeb.Controllers
{
    public class SociosController : Controller
    {
        private BibliotecaContext db;
        private RepoGenerico<Socio> repo;
        private RepoGenerico<CuentaSocio> repocuentasocio;
        private RepoGenerico<Reserva> reporeserva;
        private RepoGenerico<Prestamo> repoprestamo;

        public SociosController() {
            this.db = new BibliotecaContext();
            this.repo = new RepoGenerico<Socio>(db);
            this.repocuentasocio = new RepoGenerico<CuentaSocio>(db);
            this.reporeserva = new RepoGenerico<Reserva>(db);
            this.repoprestamo = new RepoGenerico<Prestamo>(db);
        }

        //
        // GET: /Socios/
        [Authorize(Roles = "Administrador")]
        public ActionResult Index(string nombre, string apellido)
        {
            IEnumerable<Socio> Socios = this.repo.Todos();
            if (!string.IsNullOrEmpty(nombre))
            {
                Socios = Socios.Where(c => c.Nombre.ToLower().Contains(nombre));
            }
            if (!string.IsNullOrEmpty(apellido))
            {
                Socios = Socios.Where(c => c.Apellido.ToLower().Contains(nombre));
            }
            return View(Socios);
        }


        //
        // GET: /Socios/Details/5
        [Authorize(Roles = "Administrador")]
        public ActionResult Details(int id = 0)
        {
            Socio socio = this.repo.Buscar(id);
            if (socio == null)
            {
                return HttpNotFound();
            }
            return View(socio);
        }

        //
        // GET: /Socios/Create
        [Authorize(Roles = "Administrador")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Socios/Create

        [HttpPost]
        [Authorize(Roles = "Administrador")]
        public ActionResult Create(Socio socio)
        {
            if (ModelState.IsValid)
            {
                this.repo.Agregar(socio);
                return RedirectToAction("Index");
            }

            return View(socio);
        }

        //
        // GET: /Socios/Edit/5
        [Authorize(Roles = "Administrador")]
        public ActionResult Edit(int id = 0)
        {
            Socio socio = this.repo.Buscar(id);
            if (socio == null)
            {
                return HttpNotFound();
            }
            return View(socio);
        }

        //
        // POST: /Socios/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrador")]
        public ActionResult Edit(Socio socio)
        {
            if (ModelState.IsValid)
            {
                //this.repo.Modificar(socio);
                return RedirectToAction("Index");
            }
            return View(socio);
        }

        //
        // Registrar 
        [Authorize(Roles = "Administrador")]
        protected override void Dispose(bool disposing)
        {
            this.repo.Dispose();
            base.Dispose(disposing);
        }


        //
        // GET: /Mi actividad/
        [Authorize(Roles = "Socio")]
        public ActionResult MiActividad(string username)
        {
            int idsocio = this.repocuentasocio.EncontrarPrimeroSegun(x => x.UserName.Equals(username)).SocioId;
            var reservas = this.reporeserva.EncontrarTodosSegun(x => x.Socio.Id == idsocio);
            var prestamos = this.repoprestamo.EncontrarTodosSegun(x => x.Socio.Id == idsocio);
            return View(new MiActividadVM(reservas, prestamos, idsocio));
        }

        [Authorize(Roles = "Socio")]
        public ActionResult CancelarReserva(int id)
        {
            Reserva reserva = this.reporeserva.Buscar(id);
            reserva.Cancelar();
            db.SaveChanges();
            return RedirectToAction("MiActividad", new { username = this.User.Identity.Name } );
        }

        [Authorize(Roles = "Socio")]
        public ActionResult CancelarPrestamo(int id)
        {
            Prestamo prestamo = this.repoprestamo.Buscar(id);
            prestamo.Cancelar();
            db.SaveChanges();
            return RedirectToAction("MiActividad", new { username = this.User.Identity.Name });
        }

        [Authorize(Roles = "Administrador")]
        public ActionResult Habilitar(int id)
        {
            Socio socio = this.repo.Buscar(id);
            socio.Habilitar();
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "Administrador")]
        public ActionResult Deshabilitar(int id)
        {
            Socio socio = this.repo.Buscar(id);
            socio.Deshabilitar();
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}